package com.zyp.test.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

public class LabelImageView extends AppCompatImageView {

    /**
     * 梯形距离左上角的长度
     */
    private static final int LABEL_LENGTH = 100;
    /**
     * 梯形斜边的长度
     */
    private static final int LABEL_HYPOTENUSE_LENGTH = 100;

    private Paint textPaint;
    private Paint backgroundPaint;
    private Path pathText;
    private Path pathBackground;
    private Paint basePaint;
    private float mWidth;
    private int mTop;
    private int mRight;
    private float mX1;
    private float mX2;
    private float mY1;
    private float mY2;


    public LabelImageView(Context context) {
        super(context);
        init();
    }

    public LabelImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LabelImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //计算路径
        calculatePath(getMeasuredWidth(), getMeasuredHeight());
        float xiebian = (float) Math.sqrt(2* Math.pow(LABEL_HYPOTENUSE_LENGTH + LABEL_LENGTH,2));
        canvas.drawPath(pathBackground, backgroundPaint);
        canvas.drawTextOnPath("Hot", pathText, (xiebian - mWidth)/2, -20, textPaint);
       // canvas.drawPath(pathText,basePaint);
    }

    @Override
    public void onDrawForeground(Canvas canvas) {
        super.onDrawForeground(canvas);
    }

    /**
     * 计算路径              x1   x2
     * ................................    distance（标签离右上角的垂直距离）
     * .                      .    .  .
     * .                        .    .. y1
     * .                          .   .
     * .                            . .
     * .                              . y2    height(标签垂直高度)
     * .                              .
     * ................................
     */
    private void calculatePath(int measuredWidth, int measuredHeight) {
        Matrix matrix = getImageMatrix();
        float values[] = new float[9];
        matrix.getValues(values);
        mTop = (int) values[5];
        mRight = measuredWidth;

        mX1 = mRight - LABEL_LENGTH - LABEL_HYPOTENUSE_LENGTH;
        mX2 = mRight - LABEL_HYPOTENUSE_LENGTH;
        mY1 = mTop + LABEL_LENGTH;
        mY2 = mTop + LABEL_LENGTH + LABEL_HYPOTENUSE_LENGTH;

        pathText.reset();
        pathText.moveTo(mX1, mTop);
        pathText.lineTo(mRight, mY2);
        pathText.close();

        pathBackground.reset();
        pathBackground.moveTo(mX1, mTop);
        pathBackground.lineTo(mX2, mTop);
        pathBackground.lineTo(mRight, mY1);
        pathBackground.lineTo(mRight, mY2);
        pathBackground.close();
    }

    private void init() {
        pathText = new Path();
        pathBackground = new Path();
        basePaint = new Paint();
        
        basePaint.setColor(Color.BLUE);
        basePaint.setStrokeWidth(5);
        basePaint.setStyle(Paint.Style.STROKE);
        
        textPaint = new Paint();
        textPaint.setTextSize(50);
        textPaint.setFakeBoldText(true);
        textPaint.setColor(Color.WHITE);
        mWidth =  textPaint.measureText("Hot");
        backgroundPaint = new Paint();
        backgroundPaint.setColor(Color.RED);
        backgroundPaint.setStyle(Paint.Style.FILL);
    }
}
